package auctionhaus



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(ListingService)
@Mock([Customer, Bid, Listing])
class ListingServiceTests {

    void testCreateListing() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.bids = new HashSet<Bid>()
        alice.listings = new HashSet<Listing>()
        def customerService = new CustomerService() 
        assert customerService.createCustomer(alice) == alice

        def listing = new Listing(name: 'volleyball', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 20)
        listing.bids = new HashSet<Bid>()
        def listingService = new ListingService()
        assert listingService.createListing(listing) == listing
    }

    void testCreateListingUnsuccessfull() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.bids = new HashSet<Bid>()
        alice.listings = new HashSet<Listing>()
        def customerService = new CustomerService()
        assert customerService.createCustomer(alice) == alice

        def listing = new Listing(name: 'volleyball', endDateTime: new Date() - 5, seller: alice, startingBidAmount: 20)
        listing.bids = new HashSet<Bid>()
        def listingService = new ListingService()
        assert listingService.createListing(listing) == "Unsuccessfull"
    }

    void testPlaceBid() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.bids = new HashSet<Bid>()
        alice.listings = new HashSet<Listing>()
        def customerService = new CustomerService()
        assert customerService.createCustomer(alice) == alice

        def listing = new Listing(name: 'volleyball', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 20)
        listing.bids = new HashSet<Bid>()
        def listingService = new ListingService()
        assert listingService.createListing(listing) == listing
        
        def bid = new Bid(bidder: alice, listing: listing , amount: 25)
        assert listingService.placeBid(bid).bid == bid
    }

    void testPlaceBidUnsuccessfull() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.bids = new HashSet<Bid>()
        alice.listings = new HashSet<Listing>()
        def customerService = new CustomerService()
        assert customerService.createCustomer(alice) == alice

        def listing = new Listing(name: 'volleyball', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 20)
        listing.bids = new HashSet<Bid>()
        def listingService = new ListingService()
        assert listingService.createListing(listing) == listing

        def bid = new Bid(bidder: alice, listing: listing , amount: 20.25)
        assert listingService.placeBid(bid).hasErrors
    }

    void testPlaceBidOnClosedListingUnsuccessfull() {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.bids = new HashSet<Bid>()
        alice.listings = new HashSet<Listing>()
        def customerService = new CustomerService()
        assert customerService.createCustomer(alice) == alice

        def listing = new Listing(name: 'volleyball', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 20)
        listing.bids = new HashSet<Bid>()
        listing.isClosed = true
        def listingService = new ListingService()
        assert listingService.createListing(listing) == listing

        def bid = new Bid(bidder: alice, listing: listing , amount: 21.00)
        assert listingService.placeBid(bid).hasErrors
    }
}
